﻿#region Imports
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using CommonUtilities.Attributes;
using CommonUtilities.Extensions;
using CommonUtilities.Util;
#endregion

namespace EntertainmentManager.Models
{
    [Serializable]
    public class SeasonInfo : UIToggleNotifier //: SeriesInfo
    {
        #region Declarations
        /// <summary>
        /// Backing field for Episodes
        /// </summary>
        private ObservableCollection<EpisodeInfo> episodes;
        #endregion Declarations

        #region Properties
        /// <summary>
        /// Series title
        /// </summary>
        [Description("Series name")]
        [DisplayName("Series")]
        [Sortable]
        [SortIndex(0)]
        public string SeriesTitle { get; set; }

        /// <summary>
        /// Season index
        /// </summary>
        [Filterable]
        [Description("Season index")]
        [DisplayName("S#")]
        [Sortable]
        [SortIndex(1)]
        public int SeasonIndex { get; set; }

        /// <summary>
        /// Name of the season (long format)
        /// </summary>
        [Description("Season name")]
        [DisplayName("Title")]
        [SortIndex(2)]
        public string SeasonNameLong
        {
            get
            {
                // return "Season " + SeasonIndex;

                // RKD
                ObservableCollection<string> fileNames = Episodes._OrderBy(ep => ep.FileName)._Select(ep => ep.FileName.ToWords().FirstOrDefault()).ToObservableCollection();
                return (fileNames.GetCount() < 2 ? "[{0}]" : "[{0}-{1}]")
                    .FormatTextFromParams(fileNames.FirstOrDefault(), fileNames.LastOrDefault()); // [s01e01-s01e24]
            }
        }

        /// <summary>
        /// Name of the season (short format)
        /// </summary>
        [Browsable(false)]
        [Description("Season index")]
        [DisplayName("S#")]
        [Sortable]
        [SortIndex(3)]
        public string SeasonNameShort { get { return "s" + SeasonIndex.PadLeft(SeasonPadLength, '0'); } } // RKD SeasonPadLength

        /// <summary>
        /// Backing field for <code>SeasonPadLength</code>
        /// </summary>
        private int seasonPadLength = 2;
        /// <summary>
        /// Pad length for season index
        /// </summary>
        [Browsable(false)]
        private int SeasonPadLength { get { return seasonPadLength; } }

        /// <summary>
        /// Relative path of season
        /// </summary>
        [Description("Relative path")]
        [DisplayName(@"..\ Path")]
        [SortIndex(5)]
        public string RelPath { get { return @"..\" + SeasonNameLong; } }

        /// <summary>
        /// Full path of season
        /// </summary>
        [Description("Full path")]
        [DisplayName("Path")]
        [SortIndex(6)]
        public string FullPath { get; set; }

        /// <summary>
        /// Collection of episodes encapsulated by season
        /// </summary>
        [Browsable(false)]
        public ObservableCollection<EpisodeInfo> Episodes
        {
            get { return episodes; }
            set
            {
                episodes = value;
                episodes._ForEach(ep => ep.RelPath = @"..\" + SeasonNameLong.AppendPath(ep.FileNameFull)); // Update relative path

                YearMinMaxPair = new MinMaxPair<int>(episodes._Select(ep => ep.OriginalAirDate.Year), new int[] { 0, 1 });
            }
        }

        /// <summary>
        /// Min-Max pair for season duration (in years)
        /// </summary>
        [Browsable(false)]
        internal MinMaxPair<int> YearMinMaxPair { get; private set; }

        /// <summary>
        /// Duration of the season (as period in years)
        /// </summary>
        [Description("Duration")]
        [SortIndex(4)]
        public string Duration
        {
            get { return MinMaxPair<int>.GenerateDuration(YearMinMaxPair); }
        }
        #endregion Properties

        #region Constructors
        /// <summary>
        /// Initializes a new default instance of <code>EntertainmentManager.Models.SeasonInfo</code>
        /// </summary>
        public SeasonInfo()
            : base()
        {

        }
        #endregion Constructors

        #region Methods
        /// <summary>
        /// Updates padding length of season index
        /// </summary>
        /// <param name="seasonPadLength">Padding length for season index</param>
        public void UpdatePadLength(int seasonPadLength = 2)
        {
            this.seasonPadLength = seasonPadLength;
        }
        #endregion Methods
    }

    [Serializable]
    public class SeasonInfoMirror
    {
        #region Properties
        public string SeriesTitle { get; set; }
        public int SeasonIndex { get; set; }
        public string SeasonNameLong { get; set; }
        public string SeasonNameShort { get; set; }
        public string RelPath { get; set; }
        public string FullPath { get; set; }
        public string Duration { get; set; }
        #endregion Properties
    }
}
